package com.dazhao.animejaphome.home;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.allenliu.versionchecklib.core.AllenChecker;
import com.allenliu.versionchecklib.core.VersionParams;
import com.dazhao.animejaphome.httpservice.APIInterface;
import com.dazhao.animejaphome.R;
import com.dazhao.animejaphome.bean.VersionUpdateBean;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class VersionActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_version);
        getSupportActionBar().setTitle("版本信息");

        TextView textView=(TextView) findViewById(R.id.version_text);
        textView.setText("当前版本为： "+getVersionName());


        Button b=(Button) findViewById(R.id.button);
//检查版本更新
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getVersionJsonAndAskUpdate();
            }
        });

        final Button b2=(Button)findViewById(R.id.button2);
//联系作者
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ClipboardManager cmb = (ClipboardManager) VersionActivity.this .getSystemService(Context.CLIPBOARD_SERVICE);

                cmb.setText("1073037146@qq.com");
                Snackbar.make(b2,"已将作者邮箱复制到剪贴板",Snackbar.LENGTH_SHORT).show();
                //Toast.makeText(VersionActivity.this, "已将作者邮箱复制到剪贴板", Toast.LENGTH_SHORT).show();
            }
        });




    }

    //版本更新的代码
    public void getVersionJsonAndAskUpdate(){

        Retrofit retrofit= new Retrofit.Builder()
                .baseUrl("http://114.67.131.62/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIInterface service = retrofit.create(APIInterface.class);

        Call<VersionUpdateBean> model = service.repo("VersionUpdate.json");//至此创建了一个 get请求 的对象

        model.enqueue(new Callback<VersionUpdateBean>() {//发送请求
            @Override
            public void onResponse(Call<VersionUpdateBean> call, Response<VersionUpdateBean> response) {
                //Toast.makeText(MainActivity.this, ""+response.body().getContent(), Toast.LENGTH_SHORT).show();

                int versionCode = response.body().getVersionCode();
                String versionName = response.body().getVersionName();
                String content = response.body().getContent();
                String url = response.body().getUrl();
                // 版本判断
                if (versionCode > getVersionCode()) {
                    // 提示更新
//                            Toast.makeText(MainActivity.this, "可以更新", Toast.LENGTH_SHORT).show();
                    //新版本的下载以及显示信息
                    VersionParams.Builder builder = new VersionParams.Builder();
                    builder.setOnlyDownload(true)
                            .setDownloadUrl(url)
                            .setDownloadAPKPath(getExternalCacheDir().getPath())
                            .setTitle("检测到新版本" + versionName)
                            .setUpdateMsg(content + "\n\n是否要更新？");
                    AllenChecker.startVersionCheck(VersionActivity.this, builder.build());
                }else {
                    Snackbar.make(getWindow().getDecorView(),"APP已经是最新版本",Snackbar.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<VersionUpdateBean> call, Throwable t) {
                Toast.makeText(VersionActivity.this, "服务器暂无响应", Toast.LENGTH_SHORT).show();
            }
        });

    }

    //获得当前的版本码
    private int getVersionCode() {
        // PackageManager管理器
        PackageManager pm = getPackageManager();
        // 获取相关信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
            // 版本号
            int version = packageInfo.versionCode;
            return version;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;

    }
    //获得当前的版本名
    private String getVersionName() {
        // PackageManager管理器
        PackageManager pm = getPackageManager();
        // 获取相关信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
            // 版本名
            String versionName=packageInfo.versionName;
            return versionName;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "0.0";

    }
}
